package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IAiTagService;
import com.mdd.admin.validate.AiTagCreateValidate;
import com.mdd.admin.validate.AiTagUpdateValidate;
import com.mdd.admin.validate.AiTagSearchValidate;
import com.mdd.admin.vo.AiTagListedVo;
import com.mdd.admin.vo.AiTagDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AiTag;
import com.mdd.common.mapper.AiTagMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * ai标签实现类
 * @author XueYong
 */
@Service
public class AiTagServiceImpl implements IAiTagService {
        
    @Resource
    AiTagMapper aiTagMapper;

    /**
     * ai标签列表
     *
     * @author XueYong
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AiTagListedVo>
     */
    @Override
    public PageResult<AiTagListedVo> list(PageValidate pageValidate, AiTagSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<AiTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        aiTagMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:sessionId@session_id:str",
            "like:title:str",
        });

        IPage<AiTag> iPage = aiTagMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AiTagListedVo> list = new LinkedList<>();
        for(AiTag item : iPage.getRecords()) {
            AiTagListedVo vo = new AiTagListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * ai标签详情
     *
     * @author XueYong
     * @param id 主键参数
     * @return AiTag
     */
    @Override
    public AiTagDetailVo detail(Integer id) {
        AiTag model = aiTagMapper.selectOne(
                new QueryWrapper<AiTag>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AiTagDetailVo vo = new AiTagDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * ai标签新增
     *
     * @author XueYong
     * @param createValidate 参数
     */
    @Override
    public void add(AiTagCreateValidate createValidate) {
        AiTag model = new AiTag();
        model.setSessionId(createValidate.getSessionId());
        model.setTitle(createValidate.getTitle());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aiTagMapper.insert(model);
    }

    /**
     * ai标签编辑
     *
     * @author XueYong
     * @param updateValidate 参数
     */
    @Override
    public void edit(AiTagUpdateValidate updateValidate) {
        AiTag model = aiTagMapper.selectOne(
                new QueryWrapper<AiTag>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setSessionId(updateValidate.getSessionId());
        model.setTitle(updateValidate.getTitle());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aiTagMapper.updateById(model);
    }

    /**
     * ai标签删除
     *
     * @author XueYong
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AiTag model = aiTagMapper.selectOne(
                new QueryWrapper<AiTag>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        aiTagMapper.delete(new QueryWrapper<AiTag>().eq("id", id));
    }

}
